//
//  FinancialReport.m
//  MUBATE
//
//  Created by sunbin on 2017/2/13.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "FinancialReport.h"
#import "LineGraph.h"
#import "PieChart.h"
#import "ProgressBar.h"
#import "HeadDateSelection.h"
#import "Dividend.h"
static NSString *PieChart_Identifier          = @"PieChart_Identifier";
static NSString *LineGraph_Identifier         = @"LineGraph_Identifier";
static NSString *ProgressBar_Identifier       = @"ProgressBar_Identifier";
static NSString *HeadDateSelection_Identifier = @"HeadDateSelection_Identifier";


@interface FinancialReport ()<HeadDateSelectionDelegate ,ProgressBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation FinancialReport
{
    NSString *selectedData;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = LocalizedString(@"Financial report");
}
- (void)viewDidLoad {
    [super viewDidLoad];


    [self registerNib];
}

- (void)registerNib{

    [self.tableView registerNib:[UINib nibWithNibName:@"LineGraph" bundle:nil] forCellReuseIdentifier:LineGraph_Identifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"PieChart" bundle:nil] forCellReuseIdentifier:PieChart_Identifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"ProgressBar" bundle:nil] forCellReuseIdentifier:ProgressBar_Identifier];
    [self.tableView  registerNib:[UINib nibWithNibName:@"HeadDateSelection" bundle:nil] forHeaderFooterViewReuseIdentifier:HeadDateSelection_Identifier];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
   NSInteger section = indexPath.section;

    switch (section) {
        case 0:{
            LineGraph *cell = [tableView dequeueReusableCellWithIdentifier:LineGraph_Identifier forIndexPath:indexPath];
            
            NSArray *ary = @[
  @[@[@"1月",@"2月",@"3月",@"4月",@"5月"] ,@[@"30",@"100",@"120",@"40",@"0"]],
  @[@[@"10月",@"11月",@"12月",@"1月",@"2月"],@[@"0",@"54",@"23",@"0",@"0"]]][arc4random()%2];
            NSMutableArray *aray = [[NSMutableArray alloc] initWithArray:ary];
            [cell lineGraphWithData:aray];
            
            return cell;
        }
            
            break;
        case 1:{
            PieChart *cell = [tableView dequeueReusableCellWithIdentifier:PieChart_Identifier forIndexPath:indexPath];
            [cell PieChartData:@{@"name":@"xiaoming",@"age":@"21"}];
            return cell;        
        }
            
            break;
        case 2:{
            ProgressBar *cell = [tableView dequeueReusableCellWithIdentifier:ProgressBar_Identifier forIndexPath:indexPath];
            [cell progressBarWithTargetAry:@[@[@"50",@"100",@"150",@"200",@"250",@"300",@"350"],@[@"30",@"60",@"90",@"120",@"150",@"180"]][arc4random_uniform(2)] rate:130.f];
            cell.delegate = self;
            return cell;
        }
            break;

        default:
            break;
    }
    
    return [UITableViewCell new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        HeadDateSelection *head = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeadDateSelection_Identifier];
        head.data.text = selectedData;
        head.delegate = self;
        return head;
    }else{
        return [UIView new];
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    switch (section) {
        case 0:
            return kscaleHeight(141.f);
            break;
        case 1:
            return kscaleHeight(235.f);
            break;
        case 2:
            return kscaleHeight(187.f);
            break;
        default:
            break;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return kscaleHeight(28.f);

    }else{
        return kscaleHeight(8.f);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}


#pragma make -HeadDateSelectionDelegate

- (void)headDateSelection:(UIButton *)btn{
    WEAKSELF;
   FTPopOverMenuConfiguration *PopOverMenuConfiguration =[FTPopOverMenuConfiguration defaultConfiguration];
    [PopOverMenuConfiguration setTintColor:[UIColor PopOverMenuBackColor]];
    [PopOverMenuConfiguration setBorderColor:[UIColor PopOverMenuBackColor]];
    [PopOverMenuConfiguration setTextAlignment:NSTextAlignmentCenter];

    
    NSArray *menuAry = @[@"withMenu1" ,@"withMenu2"];
    [FTPopOverMenu showForSender:btn withMenu:menuAry doneBlock:^(NSInteger selectedIndex) {
   
        //传接口 刷新数据 头部日期需要记录下来保存使用
        selectedData = [menuAry objectAtIndex:selectedIndex];
        
        [weakSelf reloadChart:selectedIndex];
        
    } dismissBlock:^{
    
    }];
}


// 选择FTPopOverMenu对应日期 需重新获取数据刷新
- (void)reloadChart:(NSInteger)index{

    [self.tableView reloadData];

}


#pragma make -ProgressBarDelegate
- (void)cehckDetail{
    Dividend *divident = [[Dividend alloc] initWithNibName:@"Dividend" bundle:nil];
    [self.navigationController pushViewController:divident animated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
